package ar.edu.caece.dns.modelo.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Properties;

import junit.framework.TestCase;
import ar.edu.caece.dns.modelo.registros.ClaseRegistro;
import ar.edu.caece.dns.modelo.registros.RegistroRecurso;
import ar.edu.caece.dns.modelo.registros.TipoRegistro;
import ar.edu.caece.dns.modelo.utils.RegistrosLoader;

public class RegistrosLoaderTestCase extends TestCase {

	private final Properties properties;

	private final String path;

	private static String fileName = "registrosLoaderTestCase.txt";

	private final String propertiesPath = "src" + File.separator + "test"
			+ File.separator + "resources" + File.separator
			+ "testCases.properties";

	public RegistrosLoaderTestCase(String name) throws IOException {
		super(name);
		properties = new Properties();

		properties.load(new FileInputStream(propertiesPath));

		path = properties.getProperty("startTestCase.path.directorioRaiz")
				.replace('/', File.separatorChar);
	}

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testCargar() {
		try {
			File directory = new File(path);

			if (!directory.exists()) {
				directory.mkdir();
			}

			File file = new File(path + File.separator + fileName);
			file.deleteOnExit();

			PrintWriter pw = new PrintWriter(file);

			pw.println("localhost;0;A;IN;127.0.0.1");
			pw.close();

			RegistrosLoader loader = new RegistrosLoader();
			loader.setPath(path);
			loader.setFileName(fileName);

			Collection<RegistroRecurso> registros = loader.cargar();

			assertNotNull(registros);
			assertTrue(registros.size() == 1);

			RegistroRecurso rr = registros.iterator().next();

			assertEquals(rr.getNombreDominio().getValor(), "localhost");
			assertEquals(rr.getTiempoVida().getValor(), 0l);
			assertEquals(rr.getTipoRegistro(), TipoRegistro.A);
			assertEquals(rr.getClaseRegistro(), ClaseRegistro.IN);
			assertEquals(rr.getValor(), "127.0.0.1");

			assertNotNull(rr.getNombreDominio().getEtiquetas());
			assertTrue(rr.getNombreDominio().getEtiquetas().length == 1);
			assertEquals("localhost", rr.getNombreDominio().get(0));

		} catch (Throwable t) {

			fail("Fallo el test de carga de registros desde archivos");
			t.printStackTrace();
		}
	}

}
